package com.bana.fvs.controller;

import javax.annotation.Resource;

import com.bana.fvs.dao.ControllerTextDAO;
import com.bana.sys.code.SendResult;
import com.bana.fvs.model.*;
import com.bana.fvs.service.ControllerService;
import com.bana.sys.socket.SocketResult;
import com.bana.sys.utils.*;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/controller", method = RequestMethod.POST)
public class ControllerController {

    @Resource
    private ControllerService controllerService;

    @Resource
    private ControllerTextDAO controllerTextDAO;

    /**
     * 获取雾区中的区域控制器集合
     * @param request wqbm 雾区编码
     * @return
     */
    @RequestMapping(value = "/getControllers.do", method = RequestMethod.POST)
    public List<Controller> getControllers(@RequestBody TokenRequest<Controller> request){
        return controllerService.getControllers(request.getData().getWqbm());
    }

    /**
     * 获取雾区中的区域控制器集合
     * @param request wqbm 雾区编码
     * @return
     */
    @RequestMapping(value = "/queryControllers.do", method = RequestMethod.POST)
    public List<Controller> queryControllers(@RequestBody TokenRequest<Controller> request){
        return  controllerService.queryControllers(request.getData().getWqbm());
    }

    /**
     * 获取控制器中的雾灯集合
     * @param request kzqbm 控制器编码
     * @return
     */
    @RequestMapping(value = "/getLamps.do", method = RequestMethod.POST)
    public Controller getLamps(@RequestBody TokenRequest<Controller> request){
        return controllerService.getControllerCache(request.getData().getKzqbm());
    }

    /**
     * 添加区域控制器
     * @param request 控制器
     * @return
     */
    @RequestMapping(value = "/insertController.do",method = RequestMethod.POST)
    public ActionResult insertController(@RequestBody TokenRequest<Controller> request) {
        ActionResult actionResult=new ActionResult();
        Controller controller=request.getData();
        controllerService.insertController(controller);
        actionResult.setCzjg(SendResult.Success.ordinal());
        actionResult.setKey(controller.getKzqbm());
        return actionResult;
    }

    /**
     *  更新区域控制器
     * @param request 控制器
     * @return
     */
    @RequestMapping(value = "/updateController.do",method = RequestMethod.POST)
    public ActionResult updateController(@RequestBody TokenRequest<Controller> request) {
        ActionResult actionResult=new ActionResult();
        controllerService.updateController(request.getData());
        actionResult.setCzjg(SendResult.Success.ordinal());
        return actionResult;
    }

    /**
     * 删除区域控制器
     * @param request kzqbm 控制器编码
     * @return
     */
    @RequestMapping(value = "/deleteController.do",method = RequestMethod.POST)
    public ActionResult deleteController(@RequestBody TokenRequest<Controller> request){
        ActionResult actionResult=new ActionResult();
        controllerService.deleteController(request.getData().getKzqbm());
        actionResult.setCzjg(SendResult.Success.ordinal());
        return actionResult;
    }

    /**
     * 上传区域控制器基本信息
     * @param request 控制器，kzqbsm
     * @return
     */
    @RequestMapping(value = "/queryController.do",method =RequestMethod.POST )
    public Controller queryController(@RequestBody TokenRequest<Controller> request) throws IOException {
        return  controllerService.queryController(request.getData());
    }

    /**
     * 下发区域控制器基本信息
     * @param request 控制器
     * @return
     */
    @RequestMapping(value = "/setController.do",method =RequestMethod.POST)
    public ActionResult setController(@RequestBody TokenRequest<Controller> request) throws IOException {
        ActionResult actionResult=new ActionResult();
        Controller controller=request.getData();
        SendResult result=controllerService.setController(controller);
        actionResult.setCzjg(result.ordinal());
        return actionResult;
    }

    /**
     * 批量下发区域控制器能见度等级参数
     * @param request 控制器和能见度等级参数
     * @return
     */
    @RequestMapping(value = "/setVisibilityScales.do",method =RequestMethod.POST)
    public List<ActionResult> setVisibilityScales(@RequestBody TokenRequest<ControllerVisibilityScaleParameter> request) throws IOException {

        List<ActionResult> list=new ArrayList<>();
        for(int i=0;i<request.getData().getKzqbsms().size();++i)
        {
            Controller controller=new Controller();
            controller.setKzqdk(request.getData().getKzqdks().get(i));
            controller.setKzqbsm(request.getData().getKzqbsms().get(i));
            SocketResult result=controllerService.setVisibilityScale(request.getToken(),controller,request.getData().getVisibilityScales());
            ActionResult actionResult=new ActionResult();
            actionResult.setKey(request.getData().getKzqbsms().get(i));
            actionResult.setCzjg(result.getResult().ordinal());
            list.add(actionResult);
        }
        return list;
    }

    /**
     * 查询控制器能见度等级参数
     * @param request 控制器
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/queryVisibilityScales.do",method =RequestMethod.POST)
    public List<VisibilityScale> queryVisibilityScales(@RequestBody TokenRequest<Controller> request) throws IOException {
        return controllerService.queryVisibilityScales(request.getToken(),request.getData().getKzqdk(),request.getData().getKzqbsm());
    }

    /**
     * 重启控制器
     * @param request 控制器
     * @return
     */
    @RequestMapping(value = "/restartController.do",method =RequestMethod.POST )
    public ActionResult restartController(@RequestBody TokenRequest<Controller> request) throws IOException {
        ActionResult actionResult=new ActionResult();
        Controller controller=request.getData();
        SocketResult result=controllerService.restartController(controller.getKzqdk(),controller.getKzqbsm(),null);
        actionResult.setCzjg(result.getResult().ordinal());
        return actionResult;
    }

    /**
     * 上传区域控制器基本信息
     * @param request 控制器，kzqbsm
     * @return
     */
    @RequestMapping(value = "/queryLamp.do",method =RequestMethod.POST )
    public Lamp queryLamp(@RequestBody TokenRequest<Lamp> request) throws IOException {
        return controllerService.queryLamp(request.getData().getKzqbm(),request.getData().getKzqdk(),request.getData().getKzqbsm(),request.getData().getWdbm());
    }

    @RequestMapping(value = "/sendCommand.do",method =RequestMethod.POST )
    public String sendCommand(@RequestBody TokenRequest<Tuple<List<Controller>,String>> request) throws IOException {
        return controllerService.sendCommands(request.getData().getItem1(),request.getData().getItem2());
    }

    /**
     * 上传区域控制器基本信息
     * @return
     */
    @RequestMapping(value = "/queryControllerTexts.do",method =RequestMethod.POST )
    public List<ControllerText> queryControllerTexts()  {
        return  controllerTextDAO.getLastDistance();
    }
}